<%@page import="org.madmac.usuario.Usuario"%>
<%@page import="org.madmac.login.Login"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
		// AQUI SERÁ A VERIFICAÇÃO SE O USUÁRIO JÁ ESTÁ LOGADO EM OUTRO SITE: UNIFY, SISTEMA DE ORDEM DE SERVIÇO.
%>
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <link rel="stylesheet" href="/madmac/menu_madmac_files/cbcscbmenu_madmac.css" type="text/css" />
		<link rel="stylesheet" href="/madmac/estilos/madmac_tables.css" type="text/css" />
      <link rel="stylesheet" href="/madmac/estilos/madmac_inputs.css" type="text/css" />
      <link rel="icon" href="/madmac/favicon.ico" type="image/x-icon" />
      <link rel="shortcut icon" href="/madmac/favicon.ico" type="image/x-icon" />
      <script type="text/javascript" src="/madmac/madmacproject.js"></script>

		<style type="text/css">
         body {
            background-image: url('/madmac/mad_main_background.png');
            background-attachment: fixed;
            background-position: right bottom;
            background-repeat: no-repeat;
         }
      </style>
      <title>Bem-vindo ao MAD MAC - beta 1.0</title>
   </head>
   <body onload="document.getElementById('username').focus()">
      <%
		try {
			if (request.getParameter("status").equals("expired")) {
      %>
      <div class="verifica_status">
         <center>
				<img src="/madmac/icons/expired.png" border="0" width="310px" align="top" style="cursor: help;"
               title=" Sua sessão expirou. Para se conectar, digite seu login e senha. " alt="">
			</center>
      </div>
      <%
			} else if (request.getParameter("status").equals("logout")) {
      %>
      <div class="verifica_status">
         <center>
				<img src="/madmac/icons/logout.png" border="0" width="310px" align="top" style="cursor: help;"
               title=" Log Out efetuado com sucesso. " alt="">
			</center>
      </div>
      <%
			} else {}
		} catch (Exception e) {
		}
      %>
      <div id="login" class="login">
         <div class="logo_login">
            <table align="center" border="0" width="60%" cellpadding="0" cellspacing="0" rules="none" class="main_table">
               <tr>
                  <td valign="top"><img src="/madmac/madmac-logo-1.0.1.png" title=" MAD MAC beta 1.0 " width="600px" alt="" /></td>
               </tr>
            </table>
         </div>
         <p></p>
         <div class="logo_login">
            <form name="login" action="/madmac/login/login.jsp" method="post" onreset="document.getElementById('username').focus()">
               <table align="center" width="60%" border="0" cellspacing="0" cellpadding="15" rules="all" class="main_table">
                  <tr>
							<td width="5%"></td>
                     <td class="meta_cell">Usuário:</td>
                     <td class="data_cell">
                        <input type="text" name="username" id="username" maxlength="100" class="input_fields" />
							</td>
							<td width="5%"></td>
                  </tr>
                  <tr>
							<td width="5%"></td>
                     <td class="meta_cell">Senha:</td>
                     <td class="data_cell">
                        <input type="password" name="passwd" id="passwd" maxlength="100" class="input_fields" />
							</td>
							<td width="5%"></td>
						</tr>
               </table>
               <p></p>
               <table width="60%" align="center" border="1" cellpadding="15" cellspacing="0" rules="rows" class="main_table">
						<tr class="menu_sql">
							<td></td>
							<td align="center">
								<input type="reset" value="Refazer" class="reset_button" />
							</td>
							<td align="center">
								<input type="submit" value="Login" class="bSubmit_login" />
							</td>
							<td></td>
						</tr>
					</table>
            </form>
         </div>
         <div class="valida_login">
         <%
		String username = request.getParameter("username");
		String passwd = request.getParameter("passwd");

		if (username != null && !username.isEmpty() && passwd != null && !passwd.isEmpty()) {
		try {
			// verificar se o usuario está cadastrado na tabela madmac.usuarios
			Login login = new Login();
			Usuario usuario = new Usuario();

			try {
				usuario = login.iniciarSessao(username, passwd);

				if (usuario != null) {

					session = request.getSession();
					session.setAttribute("usuario", usuario);
					session.setMaxInactiveInterval(60 * 60 * 2);

					response.sendRedirect("/madmac/index.jsp");
				} else {
					// MENSAGEM AO USUÁRIO: LOGIN MAL SUCEDIDO
				%>
            <p><br/></p>
            <table width="50%" align="center" border="1" cellpadding="0" cellspacing="0" rules="none" class="main_class">
               <tr class="alert">
                  <td align="center"><img src="/madmac/icons/file_alert.png" align="middle" width="64px" alt=""></td>
                  <td>Login ou Senha Incorretos. Por favor tente de novo.</td>
               </tr>
            </table>
            <%
				}
			 } catch (Exception e) {
            %>
            <p><br/></p>
            <table width="44%" align="center" border="1" cellpadding="0" cellspacing="0" rules="rows" class="main_table">
               <tr class="alert_cell">
                  <td align="center"><img src="/madmac/icons/file_alert.png" align="middle" width="64px" alt=""></td>
                  <td><%=e%></td>
               </tr>
            </table>
            <%
			} // FIM DO CATCH INTERNO
		} catch (Exception e) {
         %>
            <p><%=e.getMessage() %></p>
         <%
		}
		}
            %>
         </div>
      </div>
      <div class="footer_madmac">
         <%@include file="/common_pages/footer.jsp" %>
      </div>
   </body>
</html>